package utils;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;


import model.Measureunit;
import dtos.MeasureunitDTO;


/**
 * Session Bean implementation class MeasureunitDAO
 * @author Radulescu Cristina
 * @author Pop Loredana
 */
@Stateless
@LocalBean
public class MeasureunitDAO implements MeasureunitDAORemote {

    /**
     * Default constructor. 
     */
	@PersistenceContext
	EntityManager em;


	@EJB
	Conv conv;
	
    public MeasureunitDAO() {
        // TODO Auto-generated constructor stub
    }

    /**
     * Method used to insert a measure unit 
     * @param Measureunit
     */
    @Override
   	public void insertMeasureunit(MeasureunitDTO measure) {
   		
    	Measureunit m=em.find(Measureunit.class, measure.getId());
    	
   		if(m==null){
   			Measureunit u=conv.fromEntityMeasureunitDTO(measure);
   			
   			em.persist(u);
   		}
   		else{
   			em.merge(m);
   		}
   		
   	}
    
    /**
     * Method used to update the Measureunit
     */
    @Override
    public void updateMeasureunit(MeasureunitDTO ud){
    	if(em.find(Measureunit.class, ud.getId())!=null)
    	  em.merge(conv.fromEntityMeasureunitDTO(ud));
    }
    
    /**
     * Method used to get a Measureunit by id
     * @return Measureunit 
     */
    @Override
	public MeasureunitDTO getMeasureunit(int id) {
    	return conv.fromEntityMeasureunit(em.find(Measureunit.class, id));
	}
    
    /**
     * Method used to delete a Measure
     */
    @Override
    public void deleteMeasureunit(MeasureunitDTO ud){
		Measureunit r = em.find(Measureunit.class, ud.getId());
        em.remove(r);
	}
    
    /**
     * Method used to get all measureunits
     * @return list
     */
    @Override
    public List<MeasureunitDTO> getAllMeasureunits() {
    	List<MeasureunitDTO> resultlist = new ArrayList<>();
    	CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Measureunit> cq = cb.createQuery(Measureunit.class);
		Root<Measureunit> measureunit = cq.from(Measureunit.class);
		cq.select(measureunit);
		TypedQuery<Measureunit> q = em.createQuery(cq);

		List<Measureunit> mlist = q.getResultList();
		for (Measureunit m : mlist) {
			resultlist.add(conv.fromEntityMeasureunit(m));
		}
		return resultlist;
    }
    
    @Override
    public List<MeasureunitDTO> getAllMeasureunitsByName(String name) {
    	List<MeasureunitDTO> resultlist = new ArrayList<>();
    	CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Measureunit> cq = cb.createQuery(Measureunit.class);
		Root<Measureunit> measureunit = cq.from(Measureunit.class);
		cq.select(measureunit);
		TypedQuery<Measureunit> q = em.createQuery(cq);

		List<Measureunit> mlist = q.getResultList();
		for (Measureunit m : mlist) {
			if (m.getName().equalsIgnoreCase(name)) {
				resultlist.add(conv.fromEntityMeasureunit(m));
			}
		}
		return resultlist;
    }
}
